package com.mx.base.language.script

import org.dom4j.DocumentHelper
import org.dom4j.io.OutputFormat
import org.dom4j.io.SAXReader
import org.dom4j.io.XMLWriter
import java.io.File
import java.io.FileOutputStream


/**
 * @author: xiao.ma
 * Date: 2022/2/8 17:47
 * Desc:
 */

object XmlHelper {

    fun readStringXml(xmlFile: File): MutableMap<String, String> {
        if (!xmlFile.exists() || !xmlFile.isFile || xmlFile.name != "strings.xml") {
            println("-->${xmlFile.name} 不是有效的strings.xml")
            return mutableMapOf()
        }
        println("正在读取${xmlFile.name}")
        val reader = SAXReader()
        val document = reader.read(xmlFile)
        val stringMap = mutableMapOf<String, String>()
        document.rootElement?.elements()?.forEach { child ->
            val name = child.attributeValue("name")
            val value = child.stringValue
            if (!name.isNullOrBlank() && !value.isNullOrEmpty()) {
                stringMap[name] = value
            }
        }
        return stringMap
    }

    fun readStringXml(xmlPath: String): MutableMap<String, String> {
        println("---->readStringXml=$xmlPath")
        return readStringXml(File(xmlPath))
    }


    fun genTargetStringsXml(
        targetXmlPath: String,
        baseXmlMap: MutableMap<String, String>,
        translateMap: MutableMap<String, String>,
        update: Boolean = false,
        fill: Boolean = false
    ) {

        //获取本地目标语言已存在的翻译
        val locTargetXmlMap = readStringXml(targetXmlPath)
        val document = DocumentHelper.createDocument()
        val resourceRoot = document.addElement("resources")
        baseXmlMap.forEach { map ->
            val name = map.key
            val value = map.value
            //根据name 获取目标语言翻译
            val targetString = locTargetXmlMap[name]
            if (targetString.isNullOrEmpty()) {
                val translate = translateMap[value]
                println("----------->update=$update---$translate")
                if (!translate.isNullOrEmpty()) {
                    resourceRoot.addElement("string").apply {
                        addAttribute("name", name)
                        text = translate
                    }
                } else if (fill) {//如果需要填充则填充参考值
                    resourceRoot.addElement("string").apply {
                        addAttribute("name", name)
                        text = value
                    }
                }
            } else {
                resourceRoot.addElement("string").apply {
                    addAttribute("name", name)
                    //如果已存在并且需要更新则更新

                    text = if (update) translateMap[value] ?: targetString else targetString
                }
            }


        }

        val targetXmlFile = File(targetXmlPath)
        if (targetXmlFile.exists()) {
            targetXmlFile.delete()
        } else {
            val parent = targetXmlFile.parentFile
            if (!parent.exists()) {
                parent.mkdirs()
            }
        }
        val outputFormat = OutputFormat.createPrettyPrint().apply {
            encoding = "UTF-8"
        }
        XMLWriter(FileOutputStream(targetXmlFile), outputFormat).run {
            write(document)
            close()
        }
        println("$targetXmlPath 生成完成")
    }
}